package org.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.sf.jsqlparser.statement.select.SubJoin;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.ktjy.demoLian.entity.SysRole;
import org.ktjy.demoLian.entity.SysUser;
import org.ktjy.demoLian.mapper.SysUserMapper;
import org.ktjy.demoLian.page.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lwj
 * @since 2024-03-07
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    SysUserMapper sysUserMapper;

    @RequestMapping("/list")
    public String getList(String usrName, String roleId, String pageIndex, HttpSession session, Model model) {

        int rid = roleId != null && roleId != "" ? Integer.parseInt(roleId) : 0;
        int count = sysUserMapper.getConut("", rid);
        int currents = pageIndex != null && pageIndex != ""?Integer.parseInt(pageIndex) : 1;
        if (currents==0){
            currents=1;
        }
        User user = new User(0, currents, 2, count);
        List<SysUser> userList = null;
        List<SysRole> roles = sysUserMapper.getRoleList();
        if (usrName != null && usrName != "") {
            userList = sysUserMapper.getAMapeList(usrName, rid, user.getPageIndex(), 2);
            count = sysUserMapper.getConut(usrName, rid);
        } else {
            userList = sysUserMapper.getAMapeList("", rid, user.getPageIndex(), 2);
            count = sysUserMapper.getConut("", rid);
        }


        int countSize = count % 2 == 0 ? count / 2 : (count / 2) + 1;
        user.setPages(countSize);
        user.setRecords(userList);
        model.addAttribute("userPager", user);
        session.setAttribute("userPager", user);
        model.addAttribute("roles", roles);
        model.addAttribute("roleId", rid);
        session.setAttribute("roles", roles);
        session.setAttribute("roleId", rid);
        model.addAttribute("usrName", usrName);
        session.setAttribute("usrName", usrName);

        return "user/list";
    }
}
